﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace VehicleRecordSystem.view
{
    /// <summary>
    /// Interaction logic for ServicePage.xaml
    /// </summary>
    public partial class ServicePage : Page
    {
        private static ServicePage instanse;
        private string errorBackgroundColor = "#EBA4B0";

        public static ServicePage Instanse
        {
            get
            {
                if (instanse == null)
                {
                    instanse = new ServicePage();
                }

                return instanse;
            }
        }

        public ServicePage()
        {
            InitializeComponent();
        }

        private void btnServicePageSave_Click(object sender, RoutedEventArgs e)
        {
            if (validate())
            {
                String meter_reading=txtMeterReading.Text;
                DateTime? reading_date = datePickerDate.SelectedDate;
                String next_meter_reading = txtNextMeterReading.Text;
                DateTime? next_reading_date = datePickerNextDate.SelectedDate;
                String description = txtDescription.Text;
                BitmapImage invoice_image = new BitmapImage(new Uri(txtInvoiceImagePath.Text));
            }
        }
        private Boolean validate()
        {
            Boolean ok=true;
            var bc = new BrushConverter();

            if (string.IsNullOrEmpty(txtMeterReading.Text))
            {
                ok = false;
                txtMeterReading.Background = (System.Windows.Media.Brush)bc.ConvertFrom(errorBackgroundColor);
            }
            if (string.IsNullOrEmpty(datePickerDate.SelectedDate.ToString()))
            {
                ok = false;
                datePickerDate.Background = (System.Windows.Media.Brush)bc.ConvertFrom(errorBackgroundColor);
            }
            if (string.IsNullOrEmpty(txtNextMeterReading.Text))
            {
                ok = false;
                txtNextMeterReading.Background = (System.Windows.Media.Brush)bc.ConvertFrom(errorBackgroundColor);
            }
            if (string.IsNullOrEmpty(datePickerNextDate.SelectedDate.ToString()))
            {
                ok = false;
                datePickerNextDate.Background = (System.Windows.Media.Brush)bc.ConvertFrom(errorBackgroundColor);
            }
            if (string.IsNullOrEmpty(txtDescription.Text))
            {
                ok = false;
                txtDescription.Background = (System.Windows.Media.Brush)bc.ConvertFrom(errorBackgroundColor);
            }
            if (!validUri(txtInvoiceImagePath.Text))
            {
                ok = false;
                txtInvoiceImagePath.Background = (System.Windows.Media.Brush)bc.ConvertFrom(errorBackgroundColor);
            }

            return ok;
        }

        private void changeBackgroundColorToNormal(object sender, RoutedEventArgs e)
        {
            var bc = new BrushConverter();

            if (sender is TextBox)
            {
                TextBox txt = (TextBox)sender;
                txt.Background = (System.Windows.Media.Brush)bc.ConvertFrom("#FFFFFFFF");
            }
            if (sender is DatePicker)
            {
                DatePicker dp = (DatePicker)sender;
                dp.Background = (System.Windows.Media.Brush)bc.ConvertFrom("#FFFFFFFF");
            }
            if (sender is ComboBox)
            {
                ComboBox dp = (ComboBox)sender;
                dp.Background = (System.Windows.Media.Brush)bc.ConvertFrom("#FFFFFFFF");
            }
        }
        private Boolean validUri(String path)
        {
            Boolean ok = true;
            try
            {
                new Uri(path);
            }
            catch
            {
                ok = false;
            }
            return ok;
        }

        private void btnInvoiceImageBrowse_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                Microsoft.Win32.OpenFileDialog openFileDialog = new Microsoft.Win32.OpenFileDialog();
                openFileDialog.ShowDialog();
                openFileDialog.Title = "Set Insurance Card Image";
                openFileDialog.Filter = "All supported graphics|*.jpg;*.jpeg;*.png|" + "JPEG (*.jpg;*.jpeg)|*.jpg;*.jpeg|" + "Portable Network Graphic (*.png)|*.png";
                openFileDialog.DefaultExt = ".jpeg";
                txtInvoiceImagePath.Text = openFileDialog.FileName;

                //ImageSource imageSource = new BitmapImage(new Uri(image_textBox.Text));
                //imageRegistrationCertificate.Source = imageSource;
            }
            catch (Exception)
            {
                MessageBox.Show("Image Uploading Error", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

    }

}
